package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.JingfeiTuikuan;
import com.ruoyi.system.service.IJingfeiTuikuanService;
import com.ruoyi.web.controller.tool.JsonResult;
import com.ruoyi.web.controller.tool.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.Map;

/**
 * 退款Controller
 * 
 * @author ruoyi
 * @date 2023-04-13
 */
@Controller
@RequestMapping("/system/tuikuan")
public class JingfeiTuikuanController extends BaseController
{
    private String prefix = "system/tuikuan";

    @Autowired
    private IJingfeiTuikuanService jingfeiTuikuanService;

    @RequiresPermissions("system:tuikuan:view")
    @GetMapping()
    public String tuikuan()
    {
        return prefix + "/tuikuan";
    }

    @GetMapping("/customtuikuan")
    public String customtuikuan()
    {
        return prefix + "/custom_tuikuan";
    }

    /**
     * 查询退款列表
     */
    @RequiresPermissions("system:tuikuan:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JingfeiTuikuan jingfeiTuikuan)
    {
        startPage();
        List<JingfeiTuikuan> list = jingfeiTuikuanService.selectJingfeiTuikuanList(jingfeiTuikuan);
        return getDataTable(list);
    }

    /**
     * 导出退款列表
     */
    @RequiresPermissions("system:tuikuan:export")
    @Log(title = "退款", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JingfeiTuikuan jingfeiTuikuan)
    {
        List<JingfeiTuikuan> list = jingfeiTuikuanService.selectJingfeiTuikuanList(jingfeiTuikuan);
        ExcelUtil<JingfeiTuikuan> util = new ExcelUtil<JingfeiTuikuan>(JingfeiTuikuan.class);
        return util.exportExcel(list, "退款数据");
    }

    /**
     * 新增退款
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存退款
     */
    @RequiresPermissions("system:tuikuan:add")
    @Log(title = "退款", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JingfeiTuikuan jingfeiTuikuan)
    {
        return toAjax(jingfeiTuikuanService.insertJingfeiTuikuan(jingfeiTuikuan));
    }

    /**
     * 修改退款
     */
    @RequiresPermissions("system:tuikuan:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        JingfeiTuikuan jingfeiTuikuan = jingfeiTuikuanService.selectJingfeiTuikuanById(id);
        mmap.put("jingfeiTuikuan", jingfeiTuikuan);
        return prefix + "/edit";
    }

    /**
     * 修改保存退款
     */
    @RequiresPermissions("system:tuikuan:edit")
    @Log(title = "退款", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JingfeiTuikuan jingfeiTuikuan)
    {
        return toAjax(jingfeiTuikuanService.updateJingfeiTuikuan(jingfeiTuikuan));
    }

    /**
     * 删除退款
     */
    @RequiresPermissions("system:tuikuan:remove")
    @Log(title = "退款", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jingfeiTuikuanService.deleteJingfeiTuikuanByIds(ids));
    }

    @Log(title = "退款", businessType = BusinessType.UPDATE)
    @PostMapping("/zhidingtuikuanjihua")
    @ResponseBody
    public JsonResult zhidingTuikuanJihua(String deptId, double zhiliu, double daifa, String tuituanyuanyin) {
        if (jingfeiTuikuanService.zhidingTuikuanJihua(deptId, zhiliu, daifa, tuituanyuanyin) == 1)
            return JsonResult.getSuccessResult("制定退款成功");
        else
            return JsonResult.getErrorResult("制定退款计划失败");
    }

    @GetMapping("/findtuikuanlist")
    @ResponseBody
    public JsonResult<Map<String, Object>> findTuikuanList(String deptId, String deptName, int status, int pageIndex, int pageSize) {

        List<Map<String, Object>> tuikuanList = jingfeiTuikuanService.findTuikuanList(deptId, deptName, status,
                pageIndex, pageSize);
        int count = jingfeiTuikuanService.countChildDeptNameAndAllByDeptId(deptId, deptName, status);
        PageInfo pageInfo = new PageInfo(pageIndex, pageSize, count);
        Map<String, Object> result = new HashMap<>();
        result.put("pageInfo", pageInfo);
        result.put("tuikuanList", tuikuanList);
        return JsonResult.getSuccessResult(result);
    }

    @Log(title = "退款", businessType = BusinessType.UPDATE)
    @PostMapping("/querentuikuan")
    @ResponseBody
    public JsonResult<String> querenTuikuan(String deptId, String ids) {
        if (jingfeiTuikuanService.querenTuikuan(deptId, ids) == 1)
            return JsonResult.getSuccessResult("确认收款成功");
        else
            return JsonResult.getErrorResult("确认收款失败");
    }

    /**
     * 余额 退款列表
     */
    @PostMapping("/tuikuanlist")
    @ResponseBody
    public JsonResult<?> tuikuanlist(@RequestParam("deptid")String ppDeptId,
                                     @RequestParam("pageindex") int ppPageIndex,
                                     @RequestParam("pagesize") int ppPageSize)
    {
        try{
            List<JingfeiTuikuan> list = jingfeiTuikuanService.selectTuikuanList(ppDeptId,ppPageIndex, ppPageSize);
            int mmCount = 0;
            mmCount = jingfeiTuikuanService.countTuikuanList(ppDeptId);
            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);
            Map<String, Object> mmMap = new HashMap<String, Object>();
            mmMap.put("tuikuanlist", list);
            mmMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmMap);
        } catch (Exception e){
            return JsonResult.getErrorResult("查询退款列表失败");
        }
    }
}
